package cn.hy.spider.club16k;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hy.spider.*;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StopWatch;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App {

    public static void main(String[] args) throws IOException {
        PictureSpider pictureSpider = new PictureSpider();
        try {
            CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
            StopWatch stopWatch = new StopWatch("exec download pic");
            String proxy = "";
            String ip = "";
            Integer port = null;
            if (StrUtil.isNotBlank(proxy)) {
                String[] split = proxy.split(":");
                ip = split[0];
                port = Integer.valueOf(split[1]);
            }
            RequestConfig requestConfig;
            if (StrUtil.isNotEmpty(ip) && port != null) {
                requestConfig = RequestConfig.custom().setSocketTimeout(14999).setConnectTimeout(15000)
                        .setConnectionRequestTimeout(14999).setProxy(new HttpHost(ip, port)).build();
            } else {
                requestConfig = RequestConfig.custom().setSocketTimeout(14999).setConnectTimeout(15000)
                        .setConnectionRequestTimeout(14999).setProxy(new HttpHost("127.0.0.1", 7890)).build();
            }
            for (int i = 299; i < 302; i++) {
                List<Picture> pictures = new ArrayList<>();
                stopWatch.start("task" + i);
                String imgApiUrl = "https://16k.club/api.php?lang=zh-cn&type=index&size=30&p={}";
                String formatApiUrl = StrUtil.format(imgApiUrl, i);
                HttpGet get = new HttpGet(formatApiUrl);
                get.setConfig(requestConfig);
                get.setHeader("User-Agent", HeaderUtil.headers[RandomUtil.randomInt(HeaderUtil.headers.length)]);
                CloseableHttpResponse response = null;
                try {
                    response = httpClient.execute(get);
                } catch (IOException e) {
                    ProxyUtil.delProxy(proxy);
                }
                Header[] referers = response.getHeaders("referer");
                System.out.println("referers = " + Arrays.toString(referers));
                String bodyStr = EntityUtils.toString(response.getEntity());
                JSONArray list = JSONUtil.parseObj(bodyStr).getJSONArray("list");
                String filePath = "E:\\folder\\16kclub";
                for (Object object : list) {
                    JSONObject jo = (JSONObject) object;
                    String alt = jo.getStr("alt");
                    String path = jo.getStr("path");
                    String id = jo.getStr("id");
                    String picDirPath = filePath + File.separator + i;
                    if (!Files.exists(Paths.get(picDirPath))) {
                        FileUtil.mkdir(picDirPath);
                    }
//                    String fullPathName = StrUtil.format(filePath + "\\\\{}_{}.{}", id, alt, FileUtil.extName(path));
                    Picture picture = new Picture(path);
//                    picture.setFullPath(fullPathName);
                    picture.setTitle(id + "_" + alt + "." + FileUtil.extName(path));
                    picture.setFilePath(picDirPath);
                    pictures.add(picture);
                }
                System.out.printf("page = %s pictures = %s", i, pictures.size());
                pictureSpider.start(pictures);
                stopWatch.stop();
                System.out.println(stopWatch.prettyPrint());
            }
        } finally {
            pictureSpider.stop();
        }


    }
}
